#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;

namespace AirWeb.Security.FileSecurity
{
	/// <summary>
	/// This class represents a file path resource
	/// </summary>
	/// 

	[Serializable]
	public class FilePathResource : IResource
	{
		/// <summary>
		/// The path the this resource represents
		/// </summary>
		string m_Path;

		/// <summary>
		/// Constructor. Creates an new resource using a virtual path 
		/// </summary>
		/// <param name="i_Path"></param>
		public FilePathResource(string i_Path)
		{
			m_Path = NormalizePath(i_Path); 
		}

		/// <summary>
		/// The real path for this resource
		/// </summary>
		public string Path
		{
			get
			{
				return m_Path;
			}
		}

		/// <summary>
		/// Checks if the supplied resource represents a Path that is within the subtree of  
		/// this resource
		/// </summary>
		/// <param name="i_other">the resource to be matched</param>
		/// <returns></returns>
		/// 
		bool IResource.Matches(object i_other)
		{
			if(this.GetType() != i_other.GetType())
				return false;

			if( ((FilePathResource)i_other).Path.IndexOf(Path) == 0)
				return true;
			else
				return false;
		}

		/// <summary>
		/// Helper function. Makes sure that the path is represented wit the righrt / symbols
		/// </summary>
		/// <param name="i_path"></param>
		/// <returns></returns>
		string NormalizePath(string i_path)
		{
			string p = i_path.ToLower();
			p = p.Replace("\\","/");

			if(p.LastIndexOf("/") != p.Length -1)
				p += "/";

			return p;
		}
	}
}
